package com.cheyun.netsalev3.viewmodel

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.Transformations
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.FollowParam
import com.cheyun.netsalev3.bean.basedata.Member
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.repository.PassengerReceptionPageRepository
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.AddClueActivity
import com.cheyun.netsalev3.view.AddDriveActivity
import com.cheyun.netsalev3.view.EditCustomStoreActivity
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyDialog
import java.io.Serializable

class PassengerReceptionActivityViewModel(var passengerReceptionRepository: PassengerReceptionPageRepository) : BaseViewModel() {
    var listdata = passengerReceptionRepository.loadAndroidData()
    var memberlist = ArrayList<Member>()
    var networkState = Transformations.switchMap(listdata) {
        passengerReceptionRepository.networkState
    }!!
    var total = Transformations.switchMap(listdata) {
        passengerReceptionRepository.total
    }!!


    var showDrive = ObservableField<Int>()
    init {
        MySharedPreferences.getBaseData()?.member?.apply {
            memberlist.addAll(this)
        }
        showDrive.set(MySharedPreferences.getLoginData()?.channel?.is_driving!!)
//        showDrive.set(0)
    }

    /**
     * 分享
     */
    fun shareView(view: View, item: Data) {
        var content = view.context

        if (content is AppCompatActivity) {
            //深拷贝
            var list = ArrayList<DialogParam>()
            for (it in memberlist) {
                var items = DialogParam(it.realname, it.uid.toString())
                items.ikey = item.id.toString()
                list.add(items)
            }
            val newFragment = MyDialog.newInstance("接待分享给", list)
            newFragment.huidia = this::myPopSetData
            newFragment.show(content.supportFragmentManager, "missiles")
        }

    }

    fun myPopSetData(item: DialogParam?) {
        item?.apply {
            passengerReceptionRepository.shareData(this.ikey, this.id)
        }
    }

    /**
     * 刷新
     */
    fun refresh() {
        passengerReceptionRepository.dataSourceFactory.sourceLiveData.value?.invalidate()
    }

    var temdata: Data? = null
    var context: Context? = null
    var follow:FollowParam?=null
    /**
     * 点击item
     */
    fun onClickItem(view: View, item: Data) {
        this.temdata = item
        this.context = view.context

        var moth: (it: FollowParam) -> Unit = {
            this.follow = it
            when (it.revkey) {
                "add" -> {
                    var intent = Intent(view.context, AddClueActivity::class.java)
                    intent.putExtra("name", item.uname)
                    intent.putExtra("phone", item.phone)
                    intent.putExtra("sex", item.sex)
                    //来源：客流接待
                    intent.putExtra("come", "keliu")
                    intent.putExtra("keliudata", item as Serializable)
                    view.context.startActivity(intent)
                }
                "bazaar" ->{
                    MyToast(it.msg)
                }
                "owner" -> {
                    MyToast(it.msg)
                }
                "check" ->{
                    MyToast(it.msg)
                }
                "follow" -> {
                    var intent = Intent(view.context, DefeatDetailsActivity::class.java)
                    intent.putExtra("tid", it.tid)
                    intent.putExtra("nkey", "archive.index")
                    //来源：客流接待
                    intent.putExtra("come", "keliu")
                    intent.putExtra("keliudata", item as Serializable)
                    view.context.startActivity(intent)
                }
                "followtip" ->{
                    var context = view.context
                    if (context is AppCompatActivity) {
                        var pop = MyContentDialog.newInstance("线索跟进", it.msg +
                                "是否跟进？")
                        pop.onClickOk = this::popOk
                        pop.show(context.supportFragmentManager, "")
                    }
                }
                else -> {
                    var context = view.context
                    if (context is AppCompatActivity) {
                        var pop = MyContentDialog.newInstance("潜客激活", "该客户电话已经存在潜客(战败)," +
                                "是否激活潜客数据？")
                        pop.onClickOk = this::popOk
                        pop.show(context.supportFragmentManager, "")
                    }
                }
            }
        }
        passengerReceptionRepository.getfollow(item.id, moth)
    }
    //点击试驾
    fun onClickDriver(view:View,item:Data){
        //如果已离店，不能进行试驾
        if(item.is_leave == 1){
            MyToast("客户已离店，无法进行试乘试驾")
            return
        }

        if(item.phone.isEmpty()){
            var intent = Intent(view.context, AddDriveActivity::class.java)
            intent.putExtra("keliuData",item)
            view.context.startActivity(intent)
            return
        }

        var moth: (d: ReserveInfo) -> Unit = {
            var intent = Intent(view.context, AddDriveActivity::class.java)
//            if(!it.id.equals("0")) {
//                intent.putExtra("data", it as Serializable)
//            }
            intent.putExtra("data", it as Serializable)
            intent.putExtra("keliuData",item)
            view.context.startActivity(intent)
        }
        passengerReceptionRepository.getScanResult(item.phone,item.id.toString(), moth)
    }


//    fun getScanResult(scanData:String){
//        var moth: (d: ReserveInfo) -> Unit = {
//            data.uname = it.uname
//            data.phone = it.phone
//            if(it.reserveseries>0) {
//                data.series = it.reserveseries
//                data.seriesname = it.reserveseriesname
//                data.brandgroup = it.reserveseries.toString()
//            }
//            data.reservetime = it.reservetime
//            data.resid = it.id
//            dataShow.notifyChange()
//        }
//        editCustomStoreRepository.getScanResult(scanData, moth)
//    }

    /**
     * 弹窗回调
     */
    fun popOk() {
        follow?.apply {
            if (context != null) {
                var intent = Intent(context, DefeatDetailsActivity::class.java)
                if(follow!!.tid.isNullOrEmpty()){
                    intent.putExtra("revkey", follow!!.revkey)
                }else {
                    intent.putExtra("tid", follow!!.tid)
//                    if(follow!!.revkey.isNullOrEmpty()){
//                        intent.putExtra("revkey", follow!!.revkey)
//                    }
                }
                if(follow!!.revkey.equals("follow") || follow!!.revkey.equals("followtip")){
                    intent.putExtra("nkey", "archive.purge")  // 跟进，订车，线索移交
                }else{
//                    intent.putExtra("nkey", "archivepot.index")  // 拨号，激活
                    //拨号，激活 由state ,revkey控制
                    intent.putExtra("nkey", "archive.purge")  // 拨号，激活
                }
                context!!.startActivity(intent)
            }

        }
    }


}